package com.zhb.sys.shiro.function.controller;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import com.zhb.sys.shiro.model.Function;
import com.zhb.core.constant.RetCode;
import com.zhb.core.utils.JsonReturn;
import com.zhb.core.utils.UUIDUtil;
import com.zhb.sys.shiro.function.service.FunctionService;
 /**
 * @ClassName: Functioncontroller。
 * @Description: Function管理Controller
 * @author: zhb
 *
 */
@RestController
@RequestMapping("/function/")
public class FunctionController {
	@Autowired
	private FunctionService functionService;
	@RequestMapping("functionInfo")
	public ModelAndView functionInfo(Function function, HttpServletRequest request) {
		List<Function> functions = functionService.selectFunctionByCondition(function);
		return new ModelAndView("shiro/function/functionInfo").addObject(
				"functionList", functions);
	}
	@RequestMapping("insert")
	public JsonReturn insert(Function function) {
		JsonReturn jsonReturn = new JsonReturn();
		// 设置ID
		function.setId(UUIDUtil.getId());
		int flag = functionService.insert(function);
		if (flag == 1) {
			jsonReturn.setRetcode(RetCode.OK_CODE);
			jsonReturn.setRetmsg("添加成功");
		} else {
			jsonReturn.setRetcode(RetCode.ERROR_CODE);
			jsonReturn.setRetmsg("添加失败");
		}
		return jsonReturn;
	}
	@RequestMapping("selectFunctionById")
	public JsonReturn selectFunctionById(String id,HttpServletRequest request) {
		JsonReturn jsonReturn = new JsonReturn();
		Function function = functionService.selectFunctionById(id);
		jsonReturn.setRetcode(RetCode.OK_CODE);
		jsonReturn.setRetmsg(function);
		return jsonReturn;
	}
	@RequestMapping("edit")
	public JsonReturn edit(Function function) {
		JsonReturn jsonReturn = new JsonReturn();
		// 设置ID
		int flag = functionService.update(function);
		if (flag == 1) {
			jsonReturn.setRetcode(RetCode.OK_CODE);
			jsonReturn.setRetmsg("修改成功");
		} else {
			jsonReturn.setRetcode(RetCode.ERROR_CODE);
			jsonReturn.setRetmsg("修改失败");
		}
		return jsonReturn;
	}
	@RequestMapping("delete")
	public JsonReturn delete(String id) {
		JsonReturn jsonReturn = new JsonReturn();
		// 设置ID
		int flag = functionService.delete(id);
		if (flag == 1) {
			jsonReturn.setRetcode(RetCode.OK_CODE);
			jsonReturn.setRetmsg("删除成功");
		} else {
			jsonReturn.setRetcode(RetCode.ERROR_CODE);
			jsonReturn.setRetmsg("删除失败");
		}
		return jsonReturn;
	}
}